#include "fig_functions.h"
#include "fighter.h"

#include "ui/ui_sprite.h"
#include "math/facing.h"

math::Vec2		FigFunctions::RotateAroundPos(const math::Vec2& Center, 
											const math::Vec2& PosToRotate,
											float Rotation,
											bool WtoH
											)
{

	math::Vec2 PFromRotation = Center - PosToRotate;
	float M = PFromRotation.Magnitude();

	PFromRotation.Normalize();
	PFromRotation.Rotate(180.0f);
	PFromRotation.Rotate(-Rotation);
	PFromRotation *= M;

	if(WtoH == false)
		PFromRotation.x *= GraphicsUtil::H2W;
	else
		PFromRotation.y *= GraphicsUtil::W2H;

	//PFromRotation.x /= GetGraphics()->GetScreenWidth();
	//PFromRotation.y /= GetGraphics()->GetScreenHeight();
	

	return Center + PFromRotation;
}


bool		FigFunctions::HitTest(ui::UISprite* pSprite, 
						  const math::Vec2& Point , 
						  const math::Vec2& SpritePos , 
						  const math::Vec2& SpriteSize)
{
	 if(!pSprite)
		return false;


	 const math::Vec2 HalfSize =  SpriteSize * 0.5f * Fighter::Instance().GetScale();
	 

	 if(Point.x > SpritePos.x +  HalfSize.x)
		 return false;

	 if(Point.x < SpritePos.x -  HalfSize.x)
		 return false;

	 if(Point.y > SpritePos.y +  HalfSize.y)
		 return false;

	 if(Point.y < SpritePos.y -  HalfSize.y)
		 return false;

	//if(!pSprite->HasMask())
	//	pSprite->CreateMask();

    pSprite->m_Pos = SpritePos - HalfSize;
	float Distance = (SpritePos - Point).Magnitude();

	if(Distance > SpriteSize.x && Distance > SpriteSize.y)
		return false;
	
	// move by rotation  
	float Rotation = math::Facing::AngleVector(SpritePos-Point);
	math::Vec2 Test = math::Facing::VectorFromAngle(Rotation-180 - pSprite->m_Rotation);
	Test = Test.Normalize() * Distance	;
	
	// if it is flip
	if(pSprite->GetSpriteInst()->FlipX)
		Test.x *= -1; 	
	
	// remove according to Centre
	Test  = SpritePos + ( Test );
	

	//if( pSprite->IsInsideMask(Test))
  	//	return true;

	float R = HalfSize.x;
	if(HalfSize.x > HalfSize.y )
		R = HalfSize.y;

	R *= 0.9f;

	if ( (SpritePos - Point).Magnitude() < R )
		return true;

	return false;
}


void			FigFunctions::ToDegree(float& Degree,float TargetDegree,float Speed)
{
	if(Speed == 0)
		return;

	float BackupTargetDegree = TargetDegree;
	bool ToLeft = true;

	if( Degree > 360)
		Degree -= 360;

	if( Degree < 0)
		Degree += 360;

	if( TargetDegree > 360)
		TargetDegree -= 360;

	if( TargetDegree < 0)
		TargetDegree += 360;


	if(TargetDegree > Degree)
		ToLeft = true;
	else 
		ToLeft = false;

	float Diff = TargetDegree - Degree;
	if(Diff > 180 || Diff < -180)
		ToLeft = !ToLeft;

	if(Diff < 0)
		Diff *= -1;

    if(Diff < Speed)
	{
		Degree = BackupTargetDegree;
		return;
	}
	
	if(ToLeft)
	{
		Degree += Speed;
		//if(Degree > TargetDegree)
		//	Degree = TargetDegree;
	}
	else
	{
		Degree -= Speed;
		//if(Degree < TargetDegree)
		//	Degree = TargetDegree;
	}


	if( Degree > 360)
		Degree -= 360;

	if( Degree < 0)
		Degree += 360;

	
}




